(function($) {
	// Progress Bar
	$.extend(MediaElementPlayer.prototype, {
		buildprogress: function(player, controls, layers, media) {

			$('<div class="mejs-time-rail">'+
                                        '<span class="mejs-time-total">'+
					'<span class="mejs-time-buffering"></span>'+
					'<span class="mejs-time-current"></span>'+
					'<span class="mejs-time-handle"></span>'+
					'<span class="mejs-time-float">' + 
						'<span class="mejs-time-float-current">00:00</span>' + 
						'<span class="mejs-time-float-corner"></span>' + 
					'</span>'+
				'</span>'+
			'</div>')
				.appendTo(controls);
				controls.find('.mejs-time-buffering').hide();

			var 
				t = this,
				total = controls.find('.mejs-time-total'),
				current  = controls.find('.mejs-time-current'),
				handle  = controls.find('.mejs-time-handle'),
				timefloat  = controls.find('.mejs-time-float'),
				timefloatcurrent  = controls.find('.mejs-time-float-current'),
				handleMouseMove = function (e) {
                                        // Mouse position relative to the object
                                        var x = e.pageX,
                                        offset = total.offset(),
                                        width = total.outerWidth(true),
                                        percentage = 0,
                                        newTime = 0,
                                        pos = 0;


					if (media.duration || t.options.duration) {
						if (x < offset.left) {
							x = offset.left;
						} else if (x > width + offset.left) {
							x = width + offset.left;
						}
						
						pos = x - offset.left;
						percentage = (pos / width);
                                                
                                                newTime = percentage * (t.options.duration ? t.options.duration : media.duration);

						// Seek to where the mouse is
						if (mouseIsDown && newTime !== t.currentTime) {
                                                        t.setCurrentTime(newTime);
                                                }

						// Position floating time box
						if (!mejs.MediaFeatures.hasTouch) {
								timefloat.css('left', pos);
								timefloatcurrent.html( mejs.Utility.secondsToTimeCode(newTime) );
								timefloat.show();
						}
					}
				},
				mouseIsDown = false,
				mouseIsOver = false;

			// Handle clicks
			total
				.bind('mousedown', function (e) {
                                    // Only handle left clicks
                                    if (e.which === 1 || e.which === 0) {
						mouseIsDown = true;
						handleMouseMove(e);
						t.globalBind('mousemove.dur', function(e) {
							handleMouseMove(e);
						});
						t.globalBind('mouseup.dur', function (e) {
							mouseIsDown = false;
							timefloat.hide();
							t.globalUnbind('.dur');
						});
						return false;
					}
				})
				.bind('mouseenter', function(e) {
					mouseIsOver = true;
					t.globalBind('mousemove.dur', function(e) {
						handleMouseMove(e);
					});
					if (!mejs.MediaFeatures.hasTouch) {
						timefloat.show();
					}
				})
				.bind('mouseleave',function(e) {
					mouseIsOver = false;
					if (!mouseIsDown) {
						t.globalUnbind('.dur');
						timefloat.hide();
					}
				});

			// Loading
			media.addEventListener('progress', function (e) {
				player.setCurrentRail(e);
			}, false);

			// Current time
			media.addEventListener('timeupdate', function(e) {
				player.setCurrentRail(e);
			}, false);
			
			
			// Store for later use
			t.total = total;
			t.current = current;
			t.handle = handle;
		},
		setCurrentRail: function() {

			var t = this;
		
                        if (t.currentTime && (t.options.duration > 0) || (t.media.duration > 0)) {
                            // Update bar
                            if (t.total && t.handle) {
                                    var 
                                            newWidth = Math.round(t.total.width() * t.currentTime / (t.options.duration ? t.options.duration : t.media.duration)),
                                            handlePos = newWidth - Math.round(t.handle.outerWidth(true) / 2);

                                    t.current.width(newWidth);
                                    t.handle.css('left', handlePos);
                            }
                        }

		}
	});
})(mejs.$);